#include <func.h>

void* start_routine(void* arg) {
    // 响应状态：ENABLE
    int oldstatus;  // 保存旧的响应状态
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstatus);

    // 响应类型：DEFERRED
    int oldtype;    // 保存旧的响应类型
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);

    printf("new thread: %lx\n", pthread_self());
    sleep(10);
    printf("new thread: sleep() end\n");

    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

    for(;;) {
        // sleep(1);  // 取消点 
        pthread_testcancel();   // 取消点
    }
}

int main(int argc, char* argv[])
{
    pthread_t tid;
    pthread_create(&tid, NULL, start_routine, NULL);
    
    sleep(1);

    pthread_cancel(tid);

    pthread_join(tid, NULL);
    printf("main thread: %lx terminated\n", tid);

    return 0;
}

